﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DblogStudio.DAL.Models;

namespace DblogStudio.BLL
{
    public partial class BizBoPhanMay
    {

        public static void GetStatisCanhBao(string maBoPhanMay, out int quaHan, out int trongHan, out int truocHan, string manv = "")
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var lsCTMDPC = db.PhanCongBaoTriDbSet.Where(e => e.MaNhanVien == manv).Select(e => e.MaChiTietMay).ToList();
                var cb = int.Parse(BizAppConfig.GetDSCanhBaoDesktop());
                if (string.IsNullOrEmpty(manv))
                    cb = 1000;
                DateTime dt = DateTime.Now.AddDays(cb);
                var ls = db.LichBaoTriDbSet.Where(e =>
                    e.ChiTietMay.BoPhanMay.MaBoPhanMay == maBoPhanMay && e.NgayBaoTri <= dt &&
                    (string.IsNullOrEmpty(manv) ? true : lsCTMDPC.Contains(e.MaChiTietMay)))
                    .ToList();
                trongHan = ls.Count(e => DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days <= 0.3 * e.ChuKy && DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days >= 0);
                quaHan = ls.Count(e => DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days > 0.3 * e.ChuKy);
                truocHan = ls.Count(e => DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days < 0);
            }
        }

        public static List<BoPhanMay> GetList(string maMaySanXuat, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return GetList(maMaySanXuat, db);
                }
            else
            {
                return db.BoPhanMayDbSet.Where(e => e.MaMaySanXuat == maMaySanXuat).OrderBy(e => e.TenBoPhanMay).ToList();
            }
        }

        /// <summary>
        /// Lấy theo ID
        /// </summary>
        /// <param name="maCoSoDaoTao"></param>
        /// <returns></returns>
        public static BoPhanMay GetByID(string maBoPhanMay, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return GetByID(maBoPhanMay, db);
                }
            else
            {
                return db.BoPhanMayDbSet.Where(e => e.MaBoPhanMay == maBoPhanMay).FirstOrDefault();
            }
        }



        /// <summary>
        /// Kiểm tra tên cơ sở đào tạo có trùng hay không
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsExistByName(string maMaySanXuat, string tenBoPhanMay)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.BoPhanMayDbSet.Where(e => e.MaMaySanXuat == maMaySanXuat && e.TenBoPhanMay == tenBoPhanMay).FirstOrDefault() != null ? true : false;
            }
        }

        public static CODE_RESULT_RETURN Add(BoPhanMay obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                if (IsExistByName(obj.MaMaySanXuat, obj.TenBoPhanMay))
                    return CODE_RESULT_RETURN.TenTrung;
                obj.MaBoPhanMay = Guid.NewGuid().ToString();
                try
                {
                    db.BoPhanMayDbSet.Add(obj);
                    db.SaveChanges();
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData<BoPhanMay>(nhatKy, null, obj.MaBoPhanMay, LOAIDOITUONG.BỘ_PHẬN_MÁY, LOAITHAOTAC.Thêm);
                    BizNhatKyHoatDong.Add(nhatKy, obj);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                catch
                {
                    return CODE_RESULT_RETURN.ThatBai;
                }
            }
        }

        public static CODE_RESULT_RETURN Update(BoPhanMay obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var bpm = db.BoPhanMayDbSet.Where(e => e.MaBoPhanMay == obj.MaBoPhanMay).FirstOrDefault();
                if (bpm != null)
                {
                    var ten = db.BoPhanMayDbSet.Where(e => e.MaMaySanXuat == obj.MaMaySanXuat && e.TenBoPhanMay == obj.TenBoPhanMay && e.MaBoPhanMay != obj.MaBoPhanMay).FirstOrDefault();
                    if (ten != null)
                        return CODE_RESULT_RETURN.TenTrung;
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData(nhatKy, bpm, obj.MaBoPhanMay, LOAIDOITUONG.BỘ_PHẬN_MÁY, LOAITHAOTAC.Chỉnh_sửa);
                    bpm.TenBoPhanMay = obj.TenBoPhanMay;
                    bpm.GhiChu = obj.GhiChu;
                    bpm.MaMaySanXuat = obj.MaMaySanXuat;
                    db.SaveChanges();
                    BizNhatKyHoatDong.Add(nhatKy, bpm);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                else
                {
                    throw new Exception("Đối tượng này không tồn tại");
                }
            }
        }

        public static CODE_RESULT_RETURN Delete(string maBoPhanMay)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var bpm = db.BoPhanMayDbSet.Where(e => e.MaBoPhanMay == maBoPhanMay).FirstOrDefault();
                if (bpm != null)
                {
                    if (!(bpm.ListChiTietMay != null &&
                        bpm.ListChiTietMay.Count > 0
                        ))
                    {
                        db.BoPhanMayDbSet.Remove(bpm);
                        db.SaveChanges();
                        NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                        BizNhatKyHoatDong.InitData<BoPhanMay>(nhatKy, bpm, bpm.MaBoPhanMay, LOAIDOITUONG.BỘ_PHẬN_MÁY, LOAITHAOTAC.Xóa);
                        BizNhatKyHoatDong.Add<CongDoanSanXuat>(nhatKy, null);
                        return CODE_RESULT_RETURN.ThanhCong;
                    }
                    else
                        return CODE_RESULT_RETURN.ThatBai;
                }
                else
                {
                    return CODE_RESULT_RETURN.ThatBai;
                }
            }
        }
    }
}
